from coin.exchange.kr_rest.product_holders import get_holder_from_product


class ProductInfoAdjuster(object):
  def __init__(self, product):
    self._product = product
    self._product_holder = get_holder_from_product(self._product)

  def floor_price(self, price):
    if price is None:
      return None
    assert price > 0, price
    adj_price = self._product_holder.get_formatter().floor_price(price)
    assert adj_price > 0, (price, adj_price)
    return adj_price

  def ceil_price(self, price):
    if price is None:
      return None
    assert price > 0, price
    adj_price = self._product_holder.get_formatter().ceil_price(price)
    assert adj_price > 0, (price, adj_price)
    return adj_price

  def floor_qty(self, qty):
    if qty is None:
      return None
    # 0 is allowed; it happens when qty is too small.
    assert qty >= 0., (qty)
    if qty > 0:
      adj_qty = self._product_holder.get_formatter().floor_qty(qty)
    else:
      adj_qty = qty
    assert adj_qty >= 0, (qty, adj_qty)
    return adj_qty

  def ceil_qty(self, qty):
    if qty is None:
      return None
    # 0 is allowed; it happens when qty is too small.
    assert qty >= 0., (qty)
    if qty > 0:
      adj_qty = self._product_holder.get_formatter().ceil_qty(qty)
    else:
      adj_qty = qty
    assert adj_qty >= 0, (qty, adj_qty)
    return adj_qty
